<template>
  <view class="form-block">
    <view class="card">
      <view class="card-body">
        <u--form class="from-card" labelPosition="left" labelWidth="90" :model="user" :rules="rules" ref="form1">
          <u-form-item label="原密码" prop="oldPassword" borderBottom>
            <u--input
             type="password"
              v-model="user.oldPassword"
              placeholder="请输入原登录密码"
              border="none"
            ></u--input>
          </u-form-item>

          <u-form-item label="新密码" prop="newPassword" borderBottom>
            <u--input
              v-model="user.newPassword"
              type="password"
              placeholder="请输入新密码"
              border="none"
            ></u--input>
          </u-form-item>
		  
		  <u-form-item label="新密码确认" prop="rePassword">
		    <u--input
		      v-model="user.rePassword"
		      type="password"
		      placeholder="请确认新密码"
		      border="none"
		    ></u--input>
		  </u-form-item>
        </u--form>
      </view>
    </view>
	
	<view style="margin-top: 20px">
	  <u-button color="#2796f2" type="primary" text="提交" @click="check"></u-button>
	</view>
  </view>
</template>

<script>
import { updatePwd } from '@/api/user'
export  default {
  data() {
    return {
		user: {newPassword: '', oldPassword: '', rePassword: ''},
		rules: {
			oldPassword: {
			  type: 'string',
			  required: true,
			  message: '请输入原密码',
			  trigger: ['blur', 'change']
			},
			newPassword: {
			  type: 'string',
			  required: true,
			  message: '请输入新密码',
			  trigger: ['blur', 'change']
			},
			rePassword: {
			  type: 'string',
			  required: true,
			  message: '请确认新密码',
			  trigger: ['blur', 'change']
			},
		}
    }
  },
  methods: {
	check(){
		const self = this
		this.$refs.form1.validate().then(res => {
			if(self.user.newPassword !== self.user.rePassword){
				uni.$u.toast('两次输入的新密码不一致')
				return
			}
			console.log('self.user.newPassword',self.user.newPassword)
			if(self.user.newPassword.length < 6 || self.user.newPassword.length > 12){
				uni.$u.toast('密码长度在6到12位之间')
				return
			}
			self.submit()
		}).catch(errors => {
			uni.$u.toast('校验失败')
		})
	},
    async submit() {
		try {
			const {data} = await updatePwd(this.user)
			if (data.code != 200) {
				uni.showToast({title: data.msg, icon: 'none'})
			} else {
				uni.showToast({title: '修改成功，请重新登录', icon: 'none'})
				this.$store.commit("logout")
				setTimeout(() => {
					uni.reLaunch({url: '/pages/login/index'})
				}, 1000);
			}
		} catch (error) {
			console.log(error)
		}
    }
  }
}
</script>

<style>
	page{
		background-color: #f6f6f6;
		color: #333333;
	}
	
	.from-card{
		margin-left: 20rpx;
		padding: 20rpx 0 20rpx 0;
	}
</style>
